package cn.anycase.demo.sample.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;

import cn.anycase.demo.base.Response;
import cn.anycase.demo.sample.entity.ThisIsSample;
import cn.anycase.demo.sample.mapper.ThisIsSampleMapper;
import cn.anycase.demo.sample.service.ThisIsSampleService;
import cn.anycase.demo.vo.SampleParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * </p>
 *
 * @author yuxiaobin
 * @date 2022/8/31
 */
@SuppressWarnings("deprecation")
@Api(value = "示例", description = "Restful示例", tags = "示例")
@RestController
@RequestMapping("/sample")
public class SampleController {

    @Autowired
    ThisIsSampleService thisIsSampleService;
    @Autowired
    ThisIsSampleMapper thisIsSampleMapper;
    @Autowired
    HttpServletRequest request;


    protected <T> Page<T> getPage() {
        String pageStr = request.getParameter("page");
        String sizeStr = request.getParameter("size");
        int page = 1;
        int size = 10;
        if (!StringUtils.isEmpty(pageStr)) {
            try {
                page = Integer.parseInt(pageStr);
            } catch (Exception e) {
                //ignored
            }
        }
        if (!StringUtils.isEmpty(sizeStr)) {
            try {
                size = Integer.parseInt(sizeStr);
            } catch (Exception e) {
                //ignored
            }
        }
        return new Page<>(page, size);
    }

    /**
     * 接口数据校验，本示例没有加，请自行添加
     *
     * @param param
     * @return
     */
    @ApiOperation(value = "创建sample记录, 自行百度如何做接口数据校验", notes = "自行百度如何做接口数据校验")
    @PostMapping("")
    public Response create(@RequestBody ThisIsSample param) {
        if (thisIsSampleService.createRecord(param)) {
            return Response.success();
        }
        return Response.fail();
    }

    @ApiOperation(value = "查询sample记录", notes = "查询sample记录", response = ThisIsSample.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页当前页,不传默认第一页", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "分页一页多少条记录", dataType = "String", paramType = "query")
    })
    @GetMapping("")
    public Response list(SampleParam param) {
        return Response.success(thisIsSampleMapper.searchList(getPage(), param));
    }

    @GetMapping("/inert")
    public Response list() {
        thisIsSampleService.sampleCrud();
        return Response.success();
    }

    /**
     * 接口数据校验，本示例没有加，请自行添加
     *
     * @param id
     * @param param
     * @return
     */
    @ApiOperation(value = "更新sample记录", notes = "自行百度如何做接口数据校验")
    @PutMapping("/{id}")
    public Response update(@PathVariable("id") String id, @RequestBody ThisIsSample param) {
        param.setId(id);
        thisIsSampleMapper.updateById(param);
        return Response.success();
    }

    @ApiOperation(value = "查看某一条具体记录", notes = "查看某一条具体记录", response = ThisIsSample.class)
    @GetMapping("/{id}")
    public Response getDetail(@PathVariable("id") String id) {
        return Response.success(thisIsSampleMapper.selectById(id));
    }

    @ApiOperation(value = "删除一条记录", notes = "删除一条记录")
    @DeleteMapping("/{id}")
    public Response deleteById(@PathVariable("id") String id) {
        thisIsSampleMapper.deleteById(id);
        return Response.success();
    }
}
